//  Coins.cpp
//  AtmKiller
//  Created by USoft on 6/6/14.
//
//

#include "Coin.h"

Coin::Coin() {
}

Coin::~Coin() {
}

bool Coin::init() {
    this->initialize();
    return true;
}

void Coin::initialize() {
    this->gravity = 0.0f;
    this->existingTime = NUM_TIME_COIN;
    /*--------------MAIN FACE------------------*/
    Face* mainFace = new Face();
    mainFace->setName((char*) FACE_MAIN);
    mainFace->initWithSpriteFrameName(ANI_COIN1);
    
    // coin1 state
    Action* coin1Action = mainFace->createState((char*) ANI_COIN_FRAME1, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN1, coin1Action);
    // coin1 state
    Action* coin2Action = mainFace->createState((char*) ANI_COIN_FRAME2, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN2, coin2Action);

    // coin1 state
    Action* coin3Action = mainFace->createState((char*) ANI_COIN_FRAME3, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN3, coin3Action);

    // coin1 state
    Action* coin4Action = mainFace->createState((char*) ANI_COIN_FRAME4, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN4, coin4Action);

    // coin1 state
    Action* coin5Action = mainFace->createState((char*) ANI_COIN_FRAME5, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN5, coin5Action);

    // coin1 state
    Action* coin6Action = mainFace->createState((char*) ANI_COIN_FRAME6, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN6, coin6Action);
    
    //coin 7 state
    Action* coin7Action = mainFace->createState((char*) ANI_COIN_FRAME7, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN7, coin7Action);
    
    //coin 8 state
    Action* coin8Action = mainFace->createState((char*) ANI_COIN_FRAME8, 1, 7, 0.3, true, false);
    mainFace->registerState((char*) STATE_COIN8, coin8Action);

    // fadeInfadeOut state
    FiniteTimeAction* fadeInAction = FadeIn::create(0.1f);
    FiniteTimeAction* fadeOutAction = FadeOut::create(0.1f);
    FiniteTimeAction* fadeAction = Sequence::create(fadeInAction, fadeOutAction, NULL);
    RepeatForever* disappearedAction = RepeatForever::create((ActionInterval*) fadeAction);
    mainFace->registerState((char*) STATE_DISAPPEARED, disappearedAction->clone());
    // set state
    mainFace->setState(coin1Action);
    
    /*------------LightColumn FACE----------------*/
    Face* lightFace = new Face();
    lightFace->setName((char*)FACE_LIGHT);
    lightFace->initWithSpriteFrameName(ANI_COIN_LIGHT_7);
    lightFace->setOOVector(Point(0,580));
    lightFace->registerState((char*) STATE_DISAPPEARED, disappearedAction->clone());
    this->faces->addObject(mainFace);
    this->faces->addObject(lightFace);
    lightFace->setVisible(false);
    
    
    RectBody* body = new RectBody(mainFace->getContentSize().width, mainFace->getContentSize().height);
    CC_SAFE_RETAIN(body);
    this->body = body;
}

void Coin::appear() {
    int positive = (rand() % 2) == 1 ? 1 : -1;
    int xRandom = (10 + Utils::random() * 12) * positive;
    int yRandom = 10 + Utils::random() * 12;
    this->getWorldBody()->SetLinearVelocity(b2Vec2(-xRandom, yRandom));
}

void Coin::update(float dt) {
    Obj::update(dt);
}